<template>
    <a-form ref='formRef' :label-col='{span : 5}' :model='formData'>
        <a-form-item style='display:none' name='id'>
            <a-input type='hidden' v-model:value='formData.id' />
        </a-form-item>
        <a-form-item label='父分类' name='parentId' :rules="[{required : true, message : '请选择父分类'}]">
                <a-tree-select show-search v-model:value='formData.parentId' :tree-data='parentOptions' :field-names='{children : "children", label : "name", value : "id"}'></a-tree-select>
        </a-form-item>
        <a-form-item label='名称' name='name' :rules="[{required : true, message : '请填写名称'}]">
            <a-input type='text' v-model:value='formData.name' show-count :maxlength="50"  />
        </a-form-item>
        <a-form-item label='编码' name='code' :rules="[{required : true, message : '请填写编码'}]">
            <a-input v-model:value='formData.code' show-count :maxlength="50" />
        </a-form-item>
        <a-form-item label='图标' name='icon'>
            <a-upload list-type='picture-card' :max-count="1" v-model:file-list='coverList' :beforeUpload='beforeUpload' :customRequest='uploadCover' accept="image/png,image/jpeg,image/gif,image/webp,image/svg+xml">
                <template v-if='coverList.length == 0'>
                    <PlusOutlined />
                </template>
            </a-upload>
        </a-form-item>
        <a-form-item label='备注' name='remark'>
            <a-input v-model:value='formData.remark' show-count :maxlength="100" />
        </a-form-item>
        <a-form-item label='排序' name='sort'>
            <a-input-number type='number' :min="1" v-model:value='formData.sort' />
        </a-form-item>
        <a-form-item label='启用' name='enable'>
            <a-switch v-model:checked='formData.enable' checked-children='启用' un-checked-children='禁用'/>
        </a-form-item>
    </a-form>
</template>

<script lang="ts" setup>
import { PlusOutlined } from '@ant-design/icons-vue';
        import { saveApi, optionsApi } from '@/api/article/Category';
import { generateCodeFromUid, recursionFn } from '@/utils';
        import { ref } from 'vue';
import { useI18nStore } from '@/store/i18n';
import { Modal } from 'ant-design-vue';
import { uploadIconApi } from '@/api/Upload';

    const i18n = useI18nStore()

    const props = defineProps<{
        row? : any;
        parentId:number;
    }>()

    const formRef = ref()

    const coverList = ref<any[]>([])

    
    const formData = ref({
        id : 0,
        code : '',
        name : '',
        parentId : 0,
        icon : '',
        remark : null,
        sort : 1,
        enable : true,
    })

    function uploadCover(e) {
        if(e.file.size > 100 * 1024) {
            Modal.error({
                title : '图标不能大于100kb'
            })
            coverList.value = []
            return false
        }

        
        uploadIconApi(e.file).then(res => {
            coverList.value[0].status = 'done'
            coverList.value[0].url = import.meta.env.B_NET_STATIC_URL + res.data
            formData.value.icon = res.data

            formRef.value.validateFields(['icon'])
        })
    }

    function beforeUpload(file:File, field:FormField) {
        return true
    }


    if(props.row) {
        for(let k in props.row) {
            if(k in formData.value) {
                formData.value[k] = props.row[k]
            }
        }

        if(props.row.icon) {
            coverList.value[0] = {
                status : 'done',
                url : import.meta.env.B_NET_STATIC_URL + props.row.icon
            }

            
        }

        
    }else {
        formData.value.parentId = props.parentId
        formData.value.code = generateCodeFromUid()
    }




    async function submit() {
        let params  = await formRef.value.validate()
        if('sort' in params && params.sort === '') {
            params.sort = 1
        }


        return saveApi(params)
    }

    const parentOptions = ref<any[]>([{id : 0, name : '顶级分类', children : []}])
    
    defineExpose({
        submit
    })

    optionsApi().then(data => {
        if(data) {
            data.forEach(item => {
                item.name = i18n.t(item.name)
            })
            parentOptions.value[0].children.push(...recursionFn(data, Math.min(...data.map(r => r.parentId)), 'id', 'parentId', 'children'))
        }
    })
</script>